package cn.maihe.elg.operation.centers.guogu.dto;

import cn.maihe.elg.operation.centers.guogu.enums.GuoGuRespCodeEnum;
import cn.maihe.elg.operation.model.dto.BaseResp;
import lombok.Getter;
import lombok.Setter;

/**
 * @Description 漳州新点响应baseDTO
 * @Author SSX
 * @Date 2025/03/18
 */
@Getter
@Setter
public class GuoGuBaseRespDTO extends BaseResp {
    /**
     * 是	请求响应码 成功1,失败0
     */
    private Integer success = 1;

    /**
     * 是	业务响应码 成功1,失败0
     */
    private String code;
    /**
     * 响应信息
     */
    private String msg;

    /**
     * 扩展信息
     */
    private String ext;

    public static GuoGuBaseRespDTO of(GuoGuRespCodeEnum respCodeEnum, String message) {
        GuoGuBaseRespDTO respDTO = new GuoGuBaseRespDTO();
        respDTO.setCode(respCodeEnum.getRespCode());
        respDTO.setMsg((message == null || message.trim().length() == 0) ? respCodeEnum.getRespDesc() : message);

        if(!GuoGuRespCodeEnum.isSuccess(respCodeEnum.getRespCode())){
            respDTO.setSuccess(0);
        }
        return respDTO;
    }

    public static <T extends GuoGuBaseRespDTO> T build(GuoGuRespCodeEnum statusEnum, String msg, Class<T> tClass) {
        try {
            T t = tClass.newInstance();
            t.setCode(statusEnum.getRespCode());
            t.setMsg(msg);
            return t;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("初始化" + tClass.getName() + "异常：" + e.getMessage(), e);
        }
    }

    public static <T extends GuoGuBaseRespDTO> T success(String msg, Class<T> tClass) {
        return build(GuoGuRespCodeEnum.SUCCESS, msg, tClass);
    }

    public static <T extends GuoGuBaseRespDTO> T error(String msg, Class<T> tClass) {
        return build(GuoGuRespCodeEnum.FAILURE, msg, tClass);
    }

    public static GuoGuBaseRespDTO success(String msg) {
        return GuoGuBaseRespDTO.of(GuoGuRespCodeEnum.SUCCESS, msg);
    }

    public static GuoGuBaseRespDTO error(String msg) {
        return GuoGuBaseRespDTO.of(GuoGuRespCodeEnum.FAILURE, msg);
    }

}
